#ifndef ACCEPTOR_H
#define ACCEPTOR_H

#include <boost/bind.hpp>
#include <boost/asio.hpp>
#include <boost/noncopyable.hpp>

using boost::asio::ip::tcp;

template<typename Handler>
class Acceptor : public boost::noncopyable
{
	
	boost::asio::io_service& service_;
	tcp::acceptor acceptor_;
public:
	Acceptor(boost::asio::io_service& service, unsigned short port) : service_(service), 
		acceptor_(service_, tcp::endpoint(tcp::v4(), port)){}

	void start()
	{	
		typename Handler::Ptr handler(new Handler(service_));
		acceptor_.async_accept(handler->socket(), 
			boost::bind(&Acceptor::accepted, this, handler, boost::asio::placeholders::error));
	}

	void accepted(typename Handler::Ptr handler, const boost::system::error_code& err)
	{
		if(!err)
		{
			handler->start();
			start();
		}
	}
};

#endif